using System;
using System.Collections.Generic;
using System.Text;

using RecruitmentSystem.Common;
using System.Data.SqlClient;

namespace RecruitmentSystem.DataAccess
{
    public partial class UserAccessor
    {
        public bool CheckAdminUser(string name, string password)
        {
            const string SpName = "SpCheckAdminUserPassword";

            try
            {
                object[] parameters = new object[]
                {
                    name,
                    password
                };

                object o = SqlHelper.ExecuteScalar(Constants.ConnectionString,
                    SpName, parameters);

                if (o == null)
                {
                    return false;
                }
            }
            catch (Exception ex)
            {
                throw new ApplicationException("Exception occurs in check password for AdminUser",
                    ex);
            }

            return true;
        }

        public bool UpdatePasswordForAdminUser(string newPassword, string oldPassword, string userId)
        {
            const string SpName = "SpChangePasswordByAdminUserId";

            try
            {
                object[] parameters = new object[]
                {
                    newPassword,
                    oldPassword,
                    userId
                };

                object o = SqlHelper.ExecuteScalar(Constants.ConnectionString,
                    SpName, parameters);

                if (o != null)
                {
                    return false;
                }
            }
            catch (Exception ex)
            {
                throw new ApplicationException("Exception occurs in change password for AdminUser",
                    ex);
            }

            return true;
        }

        public AdminUser GetAdminUserById(string id)
        {
            const string SpName = "SpGetAdminUser";
            AdminUser user = null;
            SqlDataReader reader = null;

            try
            {
                reader = SqlHelper.ExecuteReader(Constants.ConnectionString,
                    SpName, new object[] { id });

                while (reader.Read())
                {
                    user = new AdminUser();
                    user.Id = reader.GetInt32(0);
                    user.Username = !reader.IsDBNull(1) ? reader.GetString(1) : null;
                    user.Password = !reader.IsDBNull(2) ? reader.GetString(2) : null;
                    user.IsAdmin = Convert.ToInt32(reader.GetString(7));
                }
            }
            catch (Exception ex)
            {
                throw new ApplicationException("Exception occurs in get admin user", ex);
            }
            finally
            {
                if (reader != null)
                {
                    reader.Close();
                }
            }

            return user;
        }
    }
}
